package niit.game;

import java.util.Random;
import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;

public class Enemy {
	GameView gv;
	MyPlane myPlane;
	static Vector<Bullet> bullet = null;
	static int x, y;
	Bitmap enemyBit, bulletEne;

	// int health;

	public Enemy(GameView gv, int x, int y, Bitmap enemyBit) {

		this.gv = gv;
		Random rnd = new Random();
		this.x = rnd.nextInt(x);
		this.y = y;
		// this.health = 50;
		this.enemyBit = enemyBit;
		bullet = new Vector<Bullet>();
		bulletEne = BitmapFactory.decodeResource(gv.getResources(),
				R.drawable.bullet4);

	}

	public void fire() {
		bullet.add(new Bullet(gv, bulletEne, x, y, 1, 1, 1, 1));
	}

	public void update() {

		this.y += 3;

		for (int i = 0; i < myPlane.bullets.size(); i++) {
			if (isCollision(this, myPlane.bullets.get(i))) {
				myPlane.bullets.remove(i);
				this.gv.enemy.remove(this);
				gv.temps.add(new Temp(gv.temps, gv, x, y,
						(Bitmap) BitmapFactory.decodeResource(
								gv.getResources(), R.drawable.paper_flower)));
				break;
			}
		}

		if (y >= gv.getHeight() || x < 0 || x >= gv.getWidth()) {
			gv.enemy.remove(this);
		}
	}

	public boolean isCollision(Enemy ene, Bullet bul) {
		return Math.sqrt(Math.pow(ene.x - bul.x + this.enemyBit.getWidth() / 2
				- bul.typeBullet.getWidth() / 2, 2)
				+ Math.pow(ene.y - bul.y + this.enemyBit.getHeight() / 2
						- bul.typeBullet.getHeight() / 2, 2)) < this.enemyBit
				.getHeight() / 2 + bul.typeBullet.getHeight() / 2;
	}

	public void onDraw(Canvas canvas) {
		update();
		canvas.drawBitmap(enemyBit, x, y, null);
	}

}
